/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  SmartRouteCalServiceImpl.java
 *
 *  Edit History:
 *      lizhangtao Created on 2022年01月11日
 *
 */
package cc.rengu.igas.route.biz.service.impl;

import cc.rengu.igas.route.common.enums.SmartRouteRspEnums;
import cc.rengu.igas.route.facade.SmartRouteCalService;
import cc.rengu.igas.route.facade.request.IntelliCalRequest;
import cc.rengu.igas.route.facade.request.IntelliQryChnlRequest;
import cc.rengu.igas.route.facade.request.IntelliStatisticsChnlRequest;
import cc.rengu.igas.route.facade.response.IntelliCalResponse;
import cc.rengu.igas.route.facade.response.IntelliQryChnlResponse;
import cc.rengu.igas.route.facade.response.IntelliStatisticsChnlResponse;
import cc.rengu.igas.route.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 补充类或接口或枚举说明
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2022/1/11
 */
public class SmartRouteCalServiceImpl implements SmartRouteCalService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    /**
     * 查询可供路由的通道信息
     *
     * @param request IntelliQryChnlRequest 请求
     * @return Result<IntelliQryChnlResponse>
     */
    @Override
    public Result<IntelliQryChnlResponse> qry(IntelliQryChnlRequest request) {
        DubboService<IntelliQryChnlRequest> dubboService = new DubboServiceImpl();
        Result<IntelliQryChnlResponse> result = new Result<>();
        try {
            DubboResult<IntelliQryChnlResponse> dubboResult = dubboService.callAtomService(request,
                    "IntelliQryChnlService", IntelliQryChnlResponse.class);
            result.setResult(dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        }catch (Exception e){
            rglog.error("查询可供路由的通道信息:<{}>", StringUtil.ExceptionToString(e));
            IntelliQryChnlResponse response = new IntelliQryChnlResponse();
            response.setHeader(request.getHeader());
            result.setResult(response);
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            }else{
                response.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                response.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 路由试算
     *
     * @param request IntelliCalRequest 请求
     * @return Result<IntelliCalResponse>
     */
    @Override
    public Result<IntelliCalResponse> cal(IntelliCalRequest request) {
        DubboService<IntelliCalRequest> dubboService = new DubboServiceImpl();
        Result<IntelliCalResponse> result = new Result<>();
        try {
            DubboResult<IntelliCalResponse> dubboResult = dubboService.callAtomService(request,
                    "IntelliCalService", IntelliCalResponse.class);
            result.setResult(dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        }catch (Exception e){
            rglog.error("路由试算:<{}>", StringUtil.ExceptionToString(e));
            IntelliCalResponse response = new IntelliCalResponse();
            response.setHeader(request.getHeader());
            result.setResult(response);
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            }else{
                response.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                response.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }

    /**
     * 路由分析监控
     *
     * @param request IntelliStatisticsChnlRequest 请求
     * @return Result<IntelliStatisticsChnlResponse>
     */
    @Override
    public Result<IntelliStatisticsChnlResponse> statistics(IntelliStatisticsChnlRequest request) {
        DubboService<IntelliStatisticsChnlRequest> dubboService = new DubboServiceImpl();
        Result<IntelliStatisticsChnlResponse> result = new Result<>();
        try {
            DubboResult<IntelliStatisticsChnlResponse> dubboResult = dubboService.callAtomService(request,
                    "IntelliStatisticsChnlService", IntelliStatisticsChnlResponse.class);
            result.setResult(dubboResult.getResult());
            result.setSuccess(dubboResult.isSuccess());
        }catch (Exception e){
            rglog.error("路由分析监控:<{}>", StringUtil.ExceptionToString(e));
            IntelliStatisticsChnlResponse response = new IntelliStatisticsChnlResponse();
            response.setHeader(request.getHeader());
            result.setResult(response);
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            }else{
                response.setRespCode(SmartRouteRspEnums.TRANS_FAIL.getRespCode());
                response.setRespDesc(SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
        }
        return result;
    }
}
